package com.ponhu.ea2.goodsCenter.provider.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ponhu.ea2.dto.PhInventoryAccessoryDto;
import com.ponhu.ea2.entity.Inventory;
import com.ponhu.ea2.entity.PhInventoryAccessory;
import com.ponhu.ea2.enums.ResponseCodeEnum;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhInventoryAccessoryMapper;
import com.ponhu.ea2.goodsCenter.provider.service.PhInventoryAccessoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.goodsCenter.provider.utils.ProBeanUtils;
import com.ponhu.ea2.support.exception.BusinessRuntimeException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 货品配件附件表  服务实现类
 *
 * @author ${author}
 * @since 2021-10-20
 */
@Service
public class PhInventoryAccessoryServiceImpl
        extends ServiceImpl<PhInventoryAccessoryMapper, PhInventoryAccessory>
        implements PhInventoryAccessoryService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editInventoryAccessories(Inventory inventory,
                                         List<PhInventoryAccessoryDto> annexList,
                                         List<PhInventoryAccessoryDto> accessoryList) {

        List<PhInventoryAccessory> phInventoryAccessories = Lists.newArrayList();

        if (CollectionUtils.isNotEmpty(annexList)) {
            phInventoryAccessories.addAll(annexList.stream().map(dto ->
                    ProBeanUtils.copyProperties(dto, PhInventoryAccessory.class)
                            .setInventoryId(inventory.getId())
                            .setInventoryCode(inventory.getInventoryCode())
                            .setCategoryId(inventory.getCategoryId())
                            .setBrandId(inventory.getBrandId())
            ).collect(Collectors.toList()));
        }

        if (CollectionUtils.isNotEmpty(accessoryList)) {
            phInventoryAccessories.addAll(accessoryList.stream().map(dto ->
                    ProBeanUtils.copyProperties(dto, PhInventoryAccessory.class)
                            .setInventoryId(inventory.getId())
                            .setInventoryCode(inventory.getInventoryCode())
                            .setCategoryId(inventory.getCategoryId())
                            .setBrandId(inventory.getBrandId())
            ).collect(Collectors.toList()));
        }

        remove(Wrappers.lambdaUpdate(PhInventoryAccessory.class)
                .eq(PhInventoryAccessory::getInventoryId, inventory.getId()));

        if (CollectionUtils.isEmpty(phInventoryAccessories)) {
            return;
        }

        if (!saveBatch(phInventoryAccessories)) {
            throw new BusinessRuntimeException(ResponseCodeEnum
                    .INVENTORY_ACCESSORY_EXCEPTION_800201);
        }
    }

    @Override
    public List<PhInventoryAccessory> getInventoryAccessorys(Long inventoryId) {

        LambdaQueryWrapper<PhInventoryAccessory> queryWrapper = Wrappers
                .lambdaQuery(PhInventoryAccessory.class)
                .eq(PhInventoryAccessory::getInventoryId, inventoryId);

        return list(queryWrapper);
    }

}
